package com.ita.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.ita.dao.MenuTypeDao;
import com.ita.pojo.MenuType;
import com.ita.util.DBUtil;

public class MenuTypeDaoImpl implements MenuTypeDao{
	
	private Logger logger = Logger.getLogger(MenuTypeDaoImpl.class);

	@Override
	public List<MenuType> loadMenuType() {
		
		String sql = "select * from food_type";
		Connection connection = null;
		PreparedStatement pst = null;
		MenuType type = null;
		ResultSet rs = null;
		List<MenuType> list = new ArrayList<MenuType>();
		try {
			connection = DBUtil.createConnection();
			pst = connection.prepareStatement(sql);
			rs = pst.executeQuery();
			while (rs.next()) {
				type = new MenuType();
				type.setId(rs.getString("id"));
				type.setName(rs.getString("type"));
				list.add(type);
			}
			return list;
		} catch (SQLException e) {
			logger.error(e.getMessage());
			return list;
		} finally {
			DBUtil.close(connection, pst, rs);
		}
	}

	@Override
	public int addMenuType(MenuType type) {
		Connection connection = null;
		PreparedStatement pst = null;
		int m  =0;
		try {
			String sql = "insert into food_type(id,type) values(?,?)";
			connection = DBUtil.createConnection();
			pst = connection.prepareStatement(sql);
			pst.setString(1, type.getId());
			pst.setString(2, type.getName());
			m = pst.executeUpdate();
			return m;
		} catch (Exception e) {
			logger.error(e.getMessage());
			return m;
		} finally {
			DBUtil.close(connection, pst, null);
		}
	}

}
